<script lang="ts" setup>
import { ref, reactive, computed } from "vue";
import { UserApi } from "@/Api/User";

interface Props {
   modelValue: boolean;
   userId?: string;
}

const props = withDefaults(defineProps<Props>(), {
   modelValue: false
});

const emit = defineEmits<{
   (e: "update:modelValue", value: boolean): void;
}>();

const dialogVisible = computed({
   get: () => props.modelValue,
   set: value => {
      if (!value) newPassword.value = "";
      emit("update:modelValue", value);
   }
});
const userId = computed(() => props.userId);

const loading = reactive({ add: false });
const disable = reactive({ add: false });
const newPassword = ref<string>();

async function handlerClick() {
   if (!userId.value) return;

   try {
      const params: User.Password.Reset = {
         userId: userId.value,
         pwd: newPassword.value
      };

      const { code } = await UserApi.Password.Reset(params);
      if (code === 200) {
         ElMessage.success("修改成功");
         dialogVisible.value = false;
      }
   } catch (error) {
      console.log("error", error);
   }
}
</script>

<template>
   <el-dialog v-model="dialogVisible" ref="dialog" title="重置密码" width="500px" draggable append-to-body>
      <el-form ref="formRef" label-width="65">
         <el-form-item label="新密码" prop="account">
            <el-input v-model="newPassword" type="password" show-password placeholder="输入新密码，不填则设置为: Ab12345678" />
         </el-form-item>
      </el-form>
      <template #footer>
         <span class="dialog-footer">
            <el-button @click="dialogVisible = false"> 取消 </el-button>
            <el-button :loading="loading.add" :disabled="disable.add" type="primary" @click="handlerClick"> 确定 </el-button>
         </span>
      </template>
   </el-dialog>
</template>

<style lang="scss" scoped>
.el-form {
   box-sizing: border-box;
   padding-inline: 20px;
}
.el-input {
   width: 100%;
}
</style>
